package ngames.com.sushitv.action;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import java.util.Arrays;

import ngames.com.sushitv.base.BaseAction;
import ngames.com.sushitv.tools.LogUtil;

/**
 * Created by Administrator on 2016/3/29.
 */
public class LoginFBUtilAction extends BaseAction {
    LoginButton mFBLoginButton;
    CallbackManager callbackManager;
    LoginFBCallBack mLoginFBCallBack;

    public LoginFBUtilAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
//        if (accessToken != null) {
//            requestFaceBookInfo(accessToken);
//        }
        LogUtil.d("LoginFBUtilAction", "accessToken = " + (accessToken == null ? "null" : accessToken.getUserId() + "," + accessToken.getToken()));
        Profile profile = Profile.getCurrentProfile();
//        profile.getId();
        LogUtil.d("LoginFBUtilAction", "profile.getId() = " + (profile == null ? "null" : profile.getName()));

        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    public void setFBLoginButton(LoginButton fbLoginButton) {
        mFBLoginButton = fbLoginButton;
        mFBLoginButton.setSoundEffectsEnabled(false);
        mFBLoginButton.setReadPermissions(Arrays.asList("user_friends", "email"));
        mFBLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Profile.fetchProfileForCurrentAccessToken();
                AccessToken accessToken = loginResult.getAccessToken();
                LogUtil.d("LoginFBUtilAction", "accessToken = " + accessToken.getUserId());
                if (mLoginFBCallBack != null) {
                    mLoginFBCallBack.onLoginFB(0, accessToken);
                }
            }

            @Override
            public void onCancel() {
                // App code
                if (mLoginFBCallBack != null) {
                    mLoginFBCallBack.onLoginFB(2, null);
                }
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                if (mLoginFBCallBack != null) {
                    mLoginFBCallBack.onLoginFB(1, null);
                }
            }
        });
    }

    public void loginFB() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken == null) {
            mFBLoginButton.performClick();
        } else {
            if (mLoginFBCallBack != null) {
                mLoginFBCallBack.onLoginFB(0, accessToken);
            }
        }
    }

    public void setLoginFBCallBack(LoginFBCallBack l) {
        mLoginFBCallBack = l;
    }

    private void getFriends() {
        new GraphRequest(AccessToken.getCurrentAccessToken(),
                "/me/friends", null, HttpMethod.GET, new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                    /* handle the result */
                LogUtil.d("LoginFBUtilAction", "getFriend obj =" + response.getJSONObject());
            }
        }).executeAsync();
    }

    public interface LoginFBCallBack {
        /**
         * result = 0成功
         * @param result
         * @param accessToken
         */
        void onLoginFB(int result, AccessToken accessToken);
    }
}
